import { defineStore } from 'pinia'
import { ref } from 'vue'
import { searchGoods } from '@/api/goods'
import { searchCategoryTwo } from '@/api/search'
import { goods } from '@/model/goodsModel'
import { categoryTwo } from '@/model/categoryModel'
export default defineStore('search', () => {
  const pagination = ref({
    currentPage: 1,
    total: 0
  })
  const changePagition = (page: number) => {
    pagination.value.currentPage = page
  }

  const goodsList = ref<goods[]>()
  const getGoodsList = async (
    page: number,
    keyword: string,
    key: number,
    categoryId?: number,
    isPromo?: boolean,
    isStock?: boolean,
    isPriceDown?: boolean
  ) => {
    const result = await searchGoods(
      page,
      keyword,
      key,
      categoryId,
      isPromo,
      isStock,
      isPriceDown
    )
    if (result.status === 200) goodsList.value = result.data
  }
  const categoryTwo = ref<categoryTwo[]>()
  const getCategoryTwo = async (keyword: string) => {
    const result = await searchCategoryTwo(keyword)
    if (result.status === 200) categoryTwo.value = result.data
  }
  return {
    pagination,
    changePagition,
    goodsList,
    getGoodsList,
    categoryTwo,
    getCategoryTwo
  }
})
